package client.view;

import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.text.SimpleDateFormat;


import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


import server.model.meeting.Participant;
import server.model.meeting.User;

public class CreateMeetingGui extends JPanel {
	
	private GuiManager manager;

	private JLabel lblInfo;
	private JLabel lblName;
	private JLabel lblOpenMeeting;
	private JLabel lblChairman;
	private JLabel lblDate;
	private JLabel lblStart;
	private JLabel lblEnd;
	private JLabel lblListInfo;
	
	private JTextField txtName;

	private JCheckBox chkOpenMeeting;
		
	private JComboBox cbbChairman;
	private JComboBox cbbDateDay;
	private JComboBox cbbDateMonth;
	private JComboBox cbbDateYear;
	
	private JComboBox cbbStartHour;
	private JComboBox cbbStartMinute;
	private JComboBox cbbEndHour;
	private JComboBox cbbEndMinute;
	
	private List lstGroups;
	private List lstParticipants;
	
	private JButton btnAddParticipants;
	private JButton btnRemoveParticipants;
	private JButton btnBack;
	private JButton btnCreateMeeting;
	private JButton open;
	
	private Iterator<User> users;
	private Iterator<Participant> participant;
	public static final String DATE_FORMAT_NOW = "yyyyMMdd";


	public CreateMeetingGui(GuiManager manager) {
		this.manager = manager;
		setLocation(0, 0);
		setSize(600, 365);
		setVisible(true);
		setLayout(null);
		

		initGUI();
	}

	private void initGUI() {
		
		lblInfo = new JLabel();
		lblInfo.setText("Creer een vergadering");
		lblInfo.setBounds(31, 12, 300, 20);
		lblInfo.setFont(new java.awt.Font("Segoe UI", 1, 14));

		lblName = new JLabel("Naam :");
		lblName.setBounds(31, 44, 100, 22);
		lblOpenMeeting = new JLabel("Open vergadering :");
		lblOpenMeeting.setBounds(31, 72, 100, 22);
		lblChairman = new JLabel("Voorzitter :");
		lblChairman.setBounds(31, 104, 100, 22);
		
		lblDate = new JLabel("Datum :");
		lblDate.setBounds(274, 44, 100, 22);
		lblStart = new JLabel("Begin tijd :");
		lblStart.setBounds(274, 72, 100, 22);
		lblEnd = new JLabel("Eind tijd :");
		lblEnd.setBounds(274, 104, 100, 22);
		
		lblListInfo = new JLabel("Kies een groep om gebruikers te selecteren");
		lblListInfo.setBounds(193, 138, 341, 16);
		
		txtName = new JTextField();
		txtName.setBounds(138, 44, 118, 22);

		chkOpenMeeting = new JCheckBox();
		chkOpenMeeting.setBounds(138, 73, 118, 22);	
				
		cbbChairman = new JComboBox();
		cbbChairman.setBounds(138, 104, 118, 22);		
		
		cbbDateDay = new JComboBox();
		cbbDateDay.setBounds(374, 44, 45, 22);
		for (int i=1; i<=31; i++){
			cbbDateDay.addItem(i);
		}
		
		cbbDateMonth = new JComboBox();
		cbbDateMonth.setBounds(425, 44, 45, 22);	
		for (int i=1; i<=12; i++){
			cbbDateMonth.addItem(i);
		}
		
		cbbDateYear = new JComboBox();
		cbbDateYear.setBounds(476, 44, 90, 22);	
		for (int i=2009; i<=2100; i++){
			cbbDateYear.addItem(i);
		}
		
		cbbStartHour = new JComboBox();
		cbbStartHour.setBounds(374, 72, 90, 22);	
		for (int i=0; i<24; i++){
			cbbStartHour.addItem(i);
		}
		
		cbbStartMinute = new JComboBox();
		cbbStartMinute.setBounds(476, 72, 90, 22);	
		for (int i=0; i<60; i++){
			cbbStartMinute.addItem(i);
		}
		
		cbbEndHour = new JComboBox();
		cbbEndHour.setBounds(374, 104, 90, 22);	
		for (int i=0; i<24; i++){
			cbbEndHour.addItem(i);
		}
		
		cbbEndMinute = new JComboBox();
		cbbEndMinute.setBounds(476, 104, 90, 22);	
		for (int i=0; i<60; i++){
			cbbEndMinute.addItem(i);
		}
		
		lstGroups = new List();
		lstGroups.setBounds(193, 166, 130, 146);
		lstGroups.setMultipleMode(true);
		
		
		lstParticipants = new List();
		lstParticipants.setBounds(436, 166, 130, 146);
		lstParticipants.setMultipleMode(true);
		
		btnAddParticipants = new JButton(">");
		btnAddParticipants.setBounds(354, 197, 50, 22);	
		btnAddParticipants.addActionListener(new btnAddParticipantsListener());
		btnRemoveParticipants = new JButton("<");
		btnRemoveParticipants.setBounds(354, 238, 50, 22);
		btnRemoveParticipants.addActionListener(new btnRemoveParticipantsListener());
		btnBack = new JButton("<");
		btnBack.setBounds(31, 332, 50, 22);
		btnBack.addActionListener(new btnBackListener());
		btnCreateMeeting = new JButton("Toevoegen");
		btnCreateMeeting.setBounds(446, 332, 120, 22);
		btnCreateMeeting.addActionListener(new btnCreateMeetingListener());
		
			      
		add(lblInfo);
		add(lblName);
		add(lblOpenMeeting);
		add(lblChairman);
		add(lblDate);
		add(lblStart);
		add(lblEnd);
		add(lblListInfo);	
		
		add(txtName);

		add(chkOpenMeeting);	
		
		add(cbbStartHour);
		add(cbbStartMinute);
		add(cbbEndHour);		
		add(cbbEndMinute);
		
		add(lstGroups);
		add(lstParticipants);
		
		add(btnAddParticipants);
		add(btnRemoveParticipants);
		add(btnBack);
		add(btnCreateMeeting);
		
		add(cbbChairman);
		add(cbbDateDay);
		add(cbbDateMonth);
		add(cbbDateYear);
		repaint();
	}
	
	  public static String now() {
		    Calendar cal = Calendar.getInstance();
		    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
		    return sdf.format(cal.getTime());
		    
		  }

	private class btnBackListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			manager.switchScreen("menuGui");
		}
	}	
	private class btnAddParticipantsListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			
			String [] selectedParticipants = lstGroups.getSelectedItems();
			boolean isUsed;
			
			for (int i=0; i<selectedParticipants.length; i++){
				isUsed = false;
				for (int j=0; j<lstParticipants.getItems().length; j++){
					if (selectedParticipants[i].matches(lstParticipants.getItems()[j])){
						isUsed = true;
					}
				}
				if(!isUsed){
					lstParticipants.add(selectedParticipants[i]);
				}
			}			
		}		
	}	
	
	private class btnRemoveParticipantsListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			String [] participants = lstParticipants.getSelectedItems();
			for (int i=0; i<participants.length; i++){
				lstParticipants.remove(participants[i]);
			}
		}		
	}	
	
	private class btnCreateMeetingListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			String name = txtName.getText();
			boolean openMeeting = chkOpenMeeting.isSelected();
			String chairman = (String) cbbChairman.getSelectedItem();
			
			String[] participants = lstParticipants.getItems(); 					
			
			Calendar start = Calendar.getInstance();
			Calendar end = Calendar.getInstance();
			
			start.set(
					Integer.parseInt(cbbDateYear.getSelectedItem().toString()), 
					Integer.parseInt(cbbDateMonth.getSelectedItem().toString()),
					Integer.parseInt(cbbDateDay.getSelectedItem().toString()),
					Integer.parseInt(cbbStartHour.getSelectedItem().toString()), 
					Integer.parseInt(cbbStartMinute.getSelectedItem().toString()));
			
			end.set(
					Integer.parseInt(cbbDateYear.getSelectedItem().toString()), 
					Integer.parseInt(cbbDateMonth.getSelectedItem().toString()),
					Integer.parseInt(cbbDateDay.getSelectedItem().toString()),
					Integer.parseInt(cbbEndHour.getSelectedItem().toString()), 
					Integer.parseInt(cbbEndMinute.getSelectedItem().toString()));
			
			
			String datumVergadering = cbbDateYear.getSelectedItem().toString() + "0" + cbbDateMonth.getSelectedItem().toString() + "" + cbbDateDay.getSelectedItem().toString();
			int datum = Integer.parseInt(datumVergadering);
			
			if(datum <= Integer.parseInt(now())){
				MsgBox message = new MsgBox(null, "Datum ligt niet in de toekomst");
				message.setSize(200, 100);
			}else{
			
				try {
					if (manager.getMeetingService().addMeeting(name, openMeeting, chairman, start.getTime(), end.getTime(), participants)){
						manager.switchScreen("menuGui");			
					}else{
						
					}
				} catch (RemoteException e) {				
					e.printStackTrace();
				}
			}
		}	
	}

	public void reset() {
		try {
			lstGroups.removeAll();
			users = manager.getAdmin().getUsers().iterator();			
			while (users.hasNext()){
				User currentUser = users.next();
				cbbChairman.addItem(currentUser.getName());
			}
			
			participant = manager.getAdmin().getParticipants().iterator();			
			while (participant.hasNext()){
				Participant currentParticipant = participant.next();
				lstGroups.add(currentParticipant.getName());
			}
			
		} catch (RemoteException e) {			
			e.printStackTrace();
		}				
	}	
}
